import {FileResult} from "@/entityType/FileResult";
import {defineComponent, PropType} from "vue";
import fileReq from "@/api/file";
import {PathUtils} from "@/utils/PathUtils";

export const CommonPropsMixin = defineComponent({
  props: {
    openFile: {
      type: String,
      default: ''
    },
    fileList: {
      type: Array as PropType<FileResult[]>,
      default: () => [] as FileResult[]
    },
    baseFilePath: {
      type: String,
      default: ''
    }
  },
  computed: {
    /**
     * 所有引入了这个mixin的显示组件都要实现这个，这样下面的两个方法就可以不用重写
     */
    comFileList() {
      return this.fileList
    },
    openFileFullPath() {
      return PathUtils.resolve(this.baseFilePath,this.openFile)
    }
  },
  methods: {
    /**
     * 显示上一个文件，方法执行之前
     */
    nextBtnClickBefore() {
      return true
    },
    /**
     * 所有引入了这个mixin的显示组件都要实现这两个方法，显示上一个文件，如果没有就是执行默认的
     */
    nextBtnClick() {
      if(this.nextBtnClickBefore() === false) {
        return
      }
      let findIndex = this.comFileList.findIndex(i=>i.fileName === this.openFile)
      findIndex++
      if(findIndex>=this.comFileList.length) {
        findIndex = 0
      }
      this.openFileSetRoute(this.comFileList[findIndex].fileName)
      this.nextBtnClickAfter()
    },
    nextBtnClickAfter() {},
    lastBtnClickBefore() {
      return true
    },
    /**
     * 所有引入了这个mixin的显示组件都要实现这两个方法，显示下一个文件，如果没有就是执行默认的
     */
    lastBtnClick() {
      if(this.lastBtnClickBefore() === false) {
        return
      }
      let findIndex = this.comFileList.findIndex(i=>i.fileName === this.openFile)
      findIndex--
      if(findIndex<0) {
        findIndex = this.comFileList.length - 1
      }
      this.openFileSetRoute(this.comFileList[findIndex].fileName)
      this.lastBtnClickAfter()
    },
    lastBtnClickAfter() {},
    openFileSetRoute(fileName) {
      this.$router.push({
        query: {
          open: fileName
        }
      })
    },
    getSrcWithName(name) {
      return fileReq.getFileUrl(PathUtils.resolve(this.baseFilePath,name))
    },
    getSrcWithName206(name) {
      return fileReq.getGetUrl(PathUtils.resolve(this.baseFilePath,name))
    },
    refreshFileList() {
      this.$emit('refreshFileList')
    }
  },
  emits: ['refreshFileList']
})
